package crashcourse;

import java.util.InputMismatchException;
import java.util.Scanner;

public class CrashCourse {

    public static void main(String[] args) {
        boolean ha=true;
        
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Opening the scanner");
        
        int arr[] = new int[10];
        System.out.println("Should we close the scanner?");
        if (keyboard.nextLine().equalsIgnoreCase("yes")) {
            System.out.println("Are you sure?");
            if(keyboard.nextLine().equalsIgnoreCase("yes"))
            {
                keyboard.close();
                
            }
        } else {
            System.out.println("Scanner is staying open.");
            
        }
        
        try {
            System.out.print("Give me a name: ");

            String name = keyboard.nextLine();
            while(ha)
            {
            try {
                System.out.println("Give me an age: ");
                int age = keyboard.nextInt();
                ha=false;

            } catch (InputMismatchException e) {
                System.out.println("STOP trying to crash the program");
                keyboard.nextLine();
                ha=true;
            }}
            ha=true;
            while (ha)
            {
            try{
            System.out.println("Give me an index less than: " + arr.length);

            int i = keyboard.nextInt();
            arr[i] = 40;
            ha=false;
            }
            catch(InputMismatchException e)
            {
            System.out.println("STOP trying to crash the program");
            }
            
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("STOP trying to crash the program");
              }
        }}
        catch (IllegalStateException e) {
            System.out.println("STOP trying to crash the program");
        }
    
        System.out.println("Thanks!");



    }

}